import json

from Crypto.Cipher import AES
import time
import hashlib
from curl_cffi import requests
import base64
from Crypto.Util.Padding import unpad, pad  # 自动补齐,和去除补齐, 使用aes加密的数据长度必须是16的倍数


def get_data(descData):
    # 变种base64
    # s = "Z21kD9ZK1ke6ugku2ccWu4n6eLnvoDT0YgGi0y3g-v0B9sYqg8L9D6UERNozYOHq411hwRVd1jCC2DE6BqQh4iv8aZAMcKeank-ZJUjtn9NmA2DFytqaTFPAM5T8UKeOtDUamLtSPaT5Qx927JQfyFSMBROtikdeivAdBRyEiu0wHpWSumZdMjFCdG9orJrdlm4VAa0EfvWw1nhnAg0GuyUXcxh0sEx-88 CwHe2oPpAFoPmaqrMa30Y4pNichfOJLh54ayaYSdnyruDbD9W0GthSMSfmU6WZcjopx5kl9lPOAcxJ7MvydYXa1gsrVCTnQICuQUZV6ueWhr3K5OLNo4v2D45k4qOSvaXtaj8cEkS43JquSrSdXLRrj4LJiVKf_FQcQ7-1 PzD_j3eJWjao_eUoLjw7VZUjco1ArAXJ1jiVobzkwkT9JujQHSUaD-hoednAlOIquGD1XzAJ83OKz2vWY_6mNUORTJuZokxMO-IMTiAucYcy2puMpWCPer-MVbwCSeP_ZaoKA9top2bwbd1_QJ3LLxU5hpJpyUlb5EWREJpUaowJWMX8SFSlO2Y1EeCgDzxBWOSGpWBKl_wAKXEK1pdkLHss_07Wk7TIgmQqB9Ben33gD-hgBl8NwCOhuY2FnWnL6on6JZ6q3fn-DHnfZyRGF5hivia3Cj6vWm9d4iBjom0K-o05tpWNEeuvxNegSMybFvz42_jiwWJwE30RuQJauoDrBbd0c_po-95 z4GjfefCmr3ygpBqKWlu3xvKdgOjcjkbMrZ_6f7ggaZDnEnLhDwxgAA1QA7fC_OcSdTBajNn0hAh4imLdjTYdUTnwH4XbF_G8_ym_YgflvWmvLbDSa1ly0T7fIKincT8BzWY-k_EgJADhVsByw5W7LbkK3v-YJIqd2EYDwrxHyWHSEwhk3DojSa74mJc_VQRNj-Vja4FtO1JiNf02BTMXf2qiWQ1qtFN62bkGOkf0CvHpFh6lvbV8GL-xNhbapjpiTcfqwS3l3GUkHZK_a0yUGytABIPHYLr4Qv34rK1vSwPWYqtU5hVyKZ2v8qNuTMdDX9QUwedTf85b2LM_FVpmPxV6IfjWj1vQMyYuHjxXj2xmD01FMbhLWIHdE4TVjPTsuRvck3UZnktuHLKwcWqGNBC7Meerud7pR_D7lAm3tJVGGuKXTaeAm5IzJVPkIY8CfvWHot2YbuwEK0GYJH9VCYqgsgvEwe_-9 NvYBm1y6f_ncoMPBEK__GS7QF3PZP69RMGn9rLCL11dLZQb_mWdGR8A8Rk6zQ4h9jFgTzC2nCeA0M7-WI4hQ64RE2T97mSgnd2Ud1aRYquseEigf9_MfwNL3eNsI29-LnUjyglWwFw-Yhu775F1pXzIheTprP1EHqtcVGgCKOY0BVdaqP8H3yj1yrAt60aEnRvXNu3TPVvA_AD3jk_SbzvOCWLKrXKa6voW2UcAs7rC7Z_Izems1oZ_cnVWDF349QduHYwG5R__2LkloQ8EJsVxFW0pRUVoda1Te2hcjTpDIJ_6mXqz0arXgihskEPAgswxD_Zk5718SeejcVbK3JI145tJAkY0J63tJzsmc5MBSqieemb2"
    # s = s.replace('-', '+').replace('_', "/")  手动格式化

    # 密钥
    ce = hashlib.md5()
    ce.update("ydsecret://query/key/B*RGygVywfNBwpmBaZg*WT7SIOUP2T0C9WHMZN39j^DAdaZhAnxvGcCY6VYFwnHl".encode())

    # 偏移量
    iv = hashlib.md5()
    iv.update("ydsecret://query/iv/C@lZe2YzHtZ2CYgaXKSVfsb7Y4QWHjITPPZ0nQp87fBeJ!Iv6v^6fvi2WN@bYpJ4".encode())

    aes = AES.new(ce.digest(), AES.MODE_CBC, iv.digest())  # 解密对象 不能通过这个对象不能同时对数据进行加解密只能使用一个
    # altchars 自动将非标准的base64格式化
    dataLIst = unpad(aes.decrypt(base64.b64decode(descData, altchars=b"-_")), 16).decode()
    print(json.loads(dataLIst).get('translateResult'))


if __name__ == "__main__":
    while True:
        n = input("请输入::::")
        haobj = hashlib.md5()
        timer = int(time.time() * 1000)
        haobj.update(f'client=fanyideskweb&mysticTime={timer}&product=webfanyi&key=fsdsogkndfokasodnaso'.encode())
        # haobj.update("client=fanyideskweb&mysticTime=1712576254381&product=webfanyi&key=fsdsogkndfokasodnaso".encode())
        sign = haobj.hexdigest()
        print(sign)
        data = {
            "i": n,
            "from": "auto",
            "to": "",
            "dictResult": True,
            "keyid": "webfanyi",
            "sign": sign,
            "client": "fanyideskweb",
            "product": "webfanyi",
            "appVersion": "1.0.0",
            "vendor": "web",
            "pointParam": "client,mysticTime,product",
            "mysticTime": timer,
            "keyfrom": "fanyi.web",
            "mid": "1",
            "screen": "1",
            "model": "1",
            "network": "wifi",
            "abtest": "0",
            "yduuid": "abcdefg"
        }
        headers = {
            "Content-Type": "application/x-www-form-urlencoded",
            "Cookie": "_ga=GA1.2.1127043010.1695912024; OUTFOX_SEARCH_USER_ID=-2055014354@10.55.164.98; OUTFOX_SEARCH_USER_ID_NCOO=515761791.9037406; DICT_DOCTRANS_SESSION_ID=NDQzNjM2YmQtNjc5Ny00NGMzLTllMzUtMWM0NjQ2YzA2MmUw",
            "Referer": "https://fanyi.youdao.com/",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
        }
        resu = requests.post('https://dict.youdao.com/webtranslate', data=data, headers=headers,
                             impersonate="chrome101")
        print(resu.text)
        get_data(resu.text)
